-module(lib_mail).
-author("Administrator").
-export([get_reward/2, del_mail/2, collect_mail/2, send_mail/2, clean_over_mail/1]).
-include("info.hrl").

feedback() ->
    ok.

add_item() ->
    ok.

clean_over_mail(Player) ->
    Mails = Player#player.mails,
    L = [X || X <- Mails, utils:unixtime() - X#mail.create_time > 0],
    Player#player{mails = L}.

check_is_over(Mail) ->
    case utils:unixtime() - Mail#mail.create_time > 0 of
        true ->
            false;
        false ->
            feedback(), %% 告诉前端已经过期 ,
            true
    end.

check_is_get(Mail) ->
    case Mail#mail.tag_get of
        get ->
            feedback(), %%    您已经领取过
            true;
        _ ->
            false
    end.

get_reward(MailId, State) ->
    X = lists:keyfind(MailId, #mail.mail_id, State#player.mails),
    case check_is_over(X) of
        true ->
            L = lists:keydelete(MailId, #mail.mail_id, State#player.mails),
            State#player{mails = L};
        false ->
            case check_is_get(X) of
                true ->
                    State;
                false ->
                    XX = lists:keystore(MailId, #mail.mail_id, State#player.mails, X#mail{tag_get = get }),
                    add_item(),     %%  由物品系统模块将物品添加到玩家
                    feedback()  , %%  向前端反馈领取成功
                    State#player{mails = XX}
            end
    end.

del_mail(MailId, State) ->
    X = lists:keyfind(MailId, #mail.mail_id, State#player.mails),
    case check_is_over(X) of
        true ->
            L = lists:keydelete(MailId, #mail.mail_id, State#player.mails),
            State#player{mails = L};
        false ->
            L = lists:keydelete(MailId, #mail.mail_id, State#player.mails),
            feedback(),   %%  向前端传输删除成功
            State#player{mails = L}
    end.

collect_mail(MailId, State) ->
    X = lists:keyfind(MailId, #mail.mail_id, State#player.mails),
    case check_is_over(X) of
        true ->
            L = lists:keydelete(MailId, #mail.mail_id, State#player.mails),
            State#player{mails = L};
        false ->
            case X#mail.tag == collect of
                true ->
                    State;
                false ->
                    XX = lists:keystore(MailId, #mail.mail_id, State#player.mails, X#mail{tag = collect}),
                    feedback(), %%  向前端传输收藏成功消息
                    State#player{mails = XX}
            end
    end.

send_mail(Mail, State) ->
    NewState = State#player{mails = [Mail | State#player.mails]},
    feedback(), %%向前端传输收到的邮件信息
    NewState.


